package com.example.beiwang.tools;

import android.widget.DatePicker;
import android.widget.TimePicker;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理工具类
 * */
public class DateUtil {
    private static  final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public static Date parseDate(DatePicker datePicker, TimePicker timePicker){
        String year = String.valueOf(datePicker.getYear());
        String month = datePicker.getMonth()>9?String.valueOf(datePicker.getMonth()):"0"+datePicker.getMonth();
        String day = datePicker.getDayOfMonth()>9?String.valueOf(datePicker.getDayOfMonth()):"0"+datePicker.getDayOfMonth();
        String hour = timePicker.getHour()>9?String.valueOf(timePicker.getHour()):"0"+timePicker.getHour();
        String minute = timePicker.getMinute()>9?String.valueOf(timePicker.getMinute()):"0"+timePicker.getMinute();
        String date = year+"-"+month+"-"+day+" "+hour+":"+minute+":00";
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseDate(DatePicker datePicker) {
        String year = String.valueOf(datePicker.getYear());
        String month = datePicker.getMonth()>9?String.valueOf(datePicker.getMonth()):"0"+datePicker.getMonth();
        String day = datePicker.getDayOfMonth()>9?String.valueOf(datePicker.getDayOfMonth()):"0"+datePicker.getDayOfMonth();
        String date = year+"-"+month+"-"+day+" 00:00:00";
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String parseDate(Date date){
        String format = sdf.format(date);
        return format;
    }

    public static DatePicker toDatePicker(Date date,DatePicker datePicker){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        datePicker.init(year,month,day,null);
        return datePicker;
    }

    public static TimePicker toTimePicker(Date date,TimePicker timePicker){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hour =  calendar.get(Calendar.HOUR_OF_DAY);
        int minute  =  calendar.get(Calendar.MINUTE);
        timePicker.setHour(hour);
        timePicker.setMinute(minute);
        return timePicker;
    }

}
